import Constant from './Constant'

export function getModeling(modeler) {
  return modeler.get('modeling')
}

export function updateElement(modeler, element, properties) {
  getModeling(modeler).updateProperties(element, properties)
}

export function removeElement(modeler, element) {
  getModeling(modeler).removeElements([element])
}

export function removeElements(modeler, elements) {
  getModeling(modeler).removeElements(elements)
}

export function getFieldValue(element, key) {
  return element.businessObject.$attrs[key]
}

export function setFieldValue(modeler, element, key, value) {
  const properties = {}
  properties[key] = value
  updateElement(modeler, element, properties)
}

export function isProcessElement(element) {
  if (!element) {
    return false
  }
  return element.type === 'bpmn:Process'
}

export function isShapeElement(element) {
  if (!element || !element.businessObject) {
    return false
  }
  return element.type === 'bpmn:Activity'
}

export function isConnectionElement(element) {
  if (!element || !element.businessObject) {
    return false
  }
  return element.type === 'bpmn:SequenceFlow'
}

export function isPreviewModel(model) {
  return model === Constant.PREVIEW_MODEL
}

export function isEditModel(model) {
  return model === Constant.EDIT_MODEL
}

export function isArray(obj) {
  return (typeof obj === 'object') && obj.constructor === Array
}

export function isNumber(obj) {
  return typeof obj === 'number' && (!isNaN(obj) || obj.constructor === Number)
}

export function isString(obj) {
  return (typeof obj === 'string') && obj.constructor === String
}

/**
 * 弹出消息
 *
 * @param {*} self
 * @param {Array} messages
 * @param {String} type
 */
export function message(self, messages, type) {
  let message = null
  if (messages && isArray(messages)) {
    for (let i = 0; messages.length > 1 && i < messages.length; i++) {
      messages[i] = i + 1 + '：' + messages[i]
    }
    message = messages.join('<br/>')
  } else {
    message = messages
  }
  self.$message({
    dangerouslyUseHTMLString: true,
    message: message,
    type: type
  })
}

/**
 * 弹出通知
 *
 * @param {*} self
 * @param {*} title
 * @param {*} messages
 * @param {*} type
 */
export function notify(self, title, messages, type) {
  let message = null
  if (messages && isArray(messages)) {
    for (let i = 0; messages.length > 1 && i < messages.length; i++) {
      messages[i] = i + 1 + '：' + messages[i]
    }
    message = messages.join('<br/>')
  } else {
    message = messages
  }
  self.$notify({
    title: title,
    dangerouslyUseHTMLString: true,
    message: message,
    position: 'top-right',
    type: type
  })
}

/**
 * 新的流程XML
 *
 * @param {*} modeler
 */
export function newProcessXml(modeler) {
  const id = modeler.get('moddle').ids.next()
  return `<?xml version="1.0" encoding="UTF-8"?>
  <bpmn:definitions>
    <bpmn:process id="Process_` + id + `" name="流程` + id + `">
    </bpmn:process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_1">
      <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_` + id + `">
      </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
  </bpmn:definitions>`
}

export default {
  getModeling, updateElement, removeElement, removeElements,
  getFieldValue, setFieldValue, isProcessElement, isShapeElement,
  isConnectionElement, isPreviewModel, isEditModel, message, notify,
  newProcessXml, isArray, isNumber, isString
}
